﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web.Security;

namespace BLL
{
    public class SystemUserBase
    {
        /// <summary>
        /// 添加系统用户
        /// </summary>
        public static string Add(Model.SystemUserBase _user)
        {
            return (new SQLServerDAL.SystemUserBase()).Add(_user);
        }
        /// <summary>
        /// 匹配ID更新系统用户信息
        /// </summary>
        public static string Update(Model.SystemUserBase _user)
        {
            return (new SQLServerDAL.SystemUserBase()).Update(_user);
        }
        /// <summary>
        /// 匹配ID获取系统用户实体信息
        /// </summary>
        public static Model.SystemUserBase GetObjById(string systemUserId)
        {
            if (Common.Utils.IsGuid(systemUserId))
            {
                Model.SystemUserBase _obj = (new SQLServerDAL.SystemUserBase()).GetObjById(systemUserId.Trim());
                _obj.RolesId = GetRolesBySystemUserId(_obj.SystemUserId);
                return _obj;
            }
            else
                return null;
        }
        /// <summary>
        /// 匹配 ID 删除系统用户
        /// isLogical = True 逻辑删除, isLogical = false 物理删除
        /// </summary>
        public static string Delete(string systemUserId, bool isLogical)
        {
            return (new SQLServerDAL.SystemUserBase()).Delete(systemUserId.Trim(), isLogical);
        }

        /// <summary>
        /// 匹配 ID 设置帐号 锁定/解锁
        /// isLockout = True 锁定, isLockout = false 解锁
        /// </summary>
        public static string UnLockout(string systemUserId, bool isLockout)
        {
            return (new SQLServerDAL.SystemUserBase()).UnLockout(systemUserId.Trim(), isLockout);
        }

        /// <summary>
        /// 匹配用户名获取系统用户实体信息
        /// </summary>
        public static Model.SystemUserBase GetObjByUsername(string username)
        {
            if (!string.IsNullOrEmpty(username))
            {
                Model.SystemUserBase _obj = (new SQLServerDAL.SystemUserBase()).GetObjByUsername(username.Trim());
                _obj.RolesId = GetRolesBySystemUserId(_obj.SystemUserId);
                return _obj;
            }
            else
                return null;
        }

        /// <summary>
        /// 验证用户名是否唯一
        /// false 可用, true 已存在
        /// </summary>
        /// <returns></returns>
        public static bool IsExisted(string username, string userid)
        {
            return (new SQLServerDAL.SystemUserBase()).IsExisted(username, userid);
        }

        /// <summary>
        /// 管理员登录验证
        /// </summary>
        /// <param name="strManagerName">用户名</param>
        /// <param name="strPassword">密码</param>
        /// <returns>验证结果</returns>
        public static string ValidateUserLogin(string usersname, string password)
        {
            string result = (new SQLServerDAL.SystemUserBase()).ValidateUserLogin(usersname, password);
            switch (result)
            {
                case "succeeded":
                    result = "succeeded";
                    break;
                default:
                    result = "faild";
                    break;
            }
            return result;
        }

        /// <summary>
        /// 匹配 ID 获取当前用户角色，多个角色 | 为间隔符
        /// </summary>
        public static string GetRolesBySystemUserId(string systemUserId)
        {
            string roles = "";

            DataTable dat = BLL.CommonBLL.Select("vw_System_UserInRoles", "LoweredRoleName, RoleId", " AND SystemUserId = '" + systemUserId + "'");
            foreach (DataRow dr in dat.Select(""))
            {
                roles += dr["RoleId"].ToString().Trim() + "|";
            }
            if (roles != "")
            {
                roles += ",";
                roles = roles.Replace("|,", "");
            }

            dat.Dispose();

            return roles;
        }

        /// <summary>
        /// 修改 Email 及其密码
        /// </summary>
        public static string ChangePassword(string username, string password)
        {
            password = FormsAuthentication.HashPasswordForStoringInConfigFile(password.Trim(), "md5");
            return (new SQLServerDAL.SystemUserBase()).ChangePassword(username, password);
        }
    }
}
